﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using SES.CMS.DO;
using SES.CMS.BL;
using SES.CMS.AdminCP;

namespace SES.CMS.Module
{
    public partial class ucMenuTop : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            this.BindDataRpt();
            this.SetActive();
        }

        public void BindDataRpt()
        {
            DataTable dt = new DataTable("dt");
            dt = new cmsCategoryBL().GetMenu(0);
            rptMenuTop.DataSource = new cmsCategoryBL().GetMenu(0);
            
            rptMenuTop.DataBind();
        }

        public void SetActive()
        {
            string P = "";
            P = Request.QueryString["Page"];
            if (P == "Trang-chu")
            {
                hplHomePage.CssClass = "Active";
                hplHomePageLeft.CssClass = "ActiveLeft";
                hplHomePageRight.CssClass = "ActiveRight";
            }
            else if (P == "Default")
            {
                hplHomePage.CssClass = "Active";
                hplHomePageLeft.CssClass = "ActiveLeft";
                hplHomePageRight.CssClass = "ActiveRight";
            }
            else
            {
                hplHomePage.CssClass = "";
                hplHomePageLeft.CssClass = "NoActiveLeft";
                hplHomePageRight.CssClass = "NoActiveRight";
            }
            if (Request.Url.AbsolutePath.Trim().ToLower().Equals("/default.aspx"))
            {
                hplHomePage.CssClass = "Active";
                hplHomePageLeft.CssClass = "ActiveLeft";
                hplHomePageRight.CssClass = "ActiveRight";
            }
        }

        protected void rptMenuTop_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemIndex < 0)
            {
                return;
            }
            DataRowView dtItem = (DataRowView)e.Item.DataItem;
            HyperLink hplLeft = (HyperLink)e.Item.FindControl("hplLeft");
            HyperLink hplMain = (HyperLink)e.Item.FindControl("hplMain");
            HyperLink hplRight = (HyperLink)e.Item.FindControl("hplRight");

            if ((hplMain != null) && (hplLeft != null) && (hplRight != null))
            {
                string PageUrl = "";
                string P = "";
                string MenuType = "0";
                
                if (dtItem["CategoryTypeID"] != null)
                {
                    MenuType = dtItem["CategoryTypeID"].ToString();
                    if (MenuType == "0")
                    {
                        hplMain.NavigateUrl = "/" + FriendlyUrl2(dtItem["Title"].ToString()) + "/Content/" + dtItem["CategoryID"].ToString() + "/Default.aspx";
                    }
                    if (MenuType == "1")
                    {
                        hplMain.NavigateUrl = "/" + FriendlyUrl2(dtItem["Title"].ToString()) + "/Articles/" + dtItem["CategoryID"].ToString() + "/Default.aspx";
                    }
                    if (MenuType == "2")
                    {
                        hplMain.NavigateUrl = "/" + FriendlyUrl2(dtItem["Title"].ToString()) + "/QuyHoach/" + dtItem["CategoryID"].ToString() + "/Default.aspx";
                    }
                    if (MenuType == "3")
                    {
                        hplMain.NavigateUrl = "/" + FriendlyUrl2(dtItem["Title"].ToString()) + "/LibsFile/" + dtItem["CategoryID"].ToString() + "/Default.aspx";
                    }
                    if (MenuType == "4")
                    {
                        hplMain.NavigateUrl = "/" + FriendlyUrl2(dtItem["Title"].ToString()) + "/Category/" + dtItem["CategoryID"].ToString() + "/Default.aspx";
                    }
                    if (MenuType == "5")
                    {
                        hplMain.NavigateUrl = "/" + FriendlyUrl2(dtItem["Title"].ToString()) + "/Category/" + dtItem["CategoryID"].ToString() + "/Default.aspx";
                    }
                    if (MenuType == "6")
                    {
                        hplMain.NavigateUrl = "/" + FriendlyUrl2(dtItem["Title"].ToString()) + "/Contact/" + dtItem["CategoryID"].ToString() + "/Default.aspx";
                    }
                }
                if (Request.QueryString["Page"] != null)
                {
                    PageUrl = Request.QueryString["Page"];
                    if (dtItem["Title"] != null)
                    {
                        P = FriendlyUrl2(dtItem["Title"].ToString());
                        if (P == PageUrl)
                        {
                            hplMain.CssClass = "Active";
                            hplLeft.CssClass = "ActiveLeft";
                            hplRight.CssClass = "ActiveRight";
                        }
                        else
                        {
                            hplMain.CssClass = "";
                            hplLeft.CssClass = "NoActiveLeft";
                            hplRight.CssClass = "NoActiveRight";
                        }
                    }
                }
                else
                {
                    hplMain.CssClass = "";
                    hplLeft.CssClass = "NoActiveLeft";
                    hplRight.CssClass = "NoActiveRight";
                }

                hplMain.Text = dtItem["Title"].ToString();
                hplMain.ToolTip = dtItem["Title"].ToString();
            }
        }

        public string FriendlyUrl(string s)
        {
            return Ultility.Change_AV(s);
        }
       
        public string FriendlyUrl2(string s)
        {
            return Ultility.Change_AV2(s);
        }
    }
}